<html>
<head><meta charset="utf-8"><title>Transmute safety question · general · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/index.html">general</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Transmute.20safety.20question.html">Transmute safety question</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="210925476"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Transmute%20safety%20question/near/210925476" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> marmeladema <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Transmute.20safety.20question.html#210925476">(Sep 22 2020 at 20:15)</a>:</h4>
<p>Let's say I have:</p>
<div class="codehilite"><pre><span></span><code>struct Test&lt;&#39;a&gt;(&amp;&#39;a [u8]);

struct TestSlice&lt;&#39;a&gt;([Test&lt;&#39;a&gt;]);
</code></pre></div>


<p>Is it safe to transmute a <code>&amp;[Test&lt;'a&gt;]</code> to a <code>&amp;TestSlice&lt;'a&gt;</code> with:</p>
<div class="codehilite"><pre><span></span><code>unsafe { std::mem::transmute::&lt;&amp;[Test&lt;&#39;a&gt;], &amp;TestSlice&lt;&#39;a&gt;&gt;(slice) }
</code></pre></div>


<p>and also the other way around?</p>
<p>Miri does not seem to complain: <a href="https://play.rust-lang.org/?version=stable&amp;mode=debug&amp;edition=2018&amp;gist=5156b9cad5a692c91f13a2bcf444891f">https://play.rust-lang.org/?version=stable&amp;mode=debug&amp;edition=2018&amp;gist=5156b9cad5a692c91f13a2bcf444891f</a></p>



<a name="210925760"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Transmute%20safety%20question/near/210925760" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Transmute.20safety.20question.html#210925760">(Sep 22 2020 at 20:17)</a>:</h4>
<p>Afaik you need  <code>#[repr(transparent)]</code> for this to be sound</p>



<a name="210925767"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Transmute%20safety%20question/near/210925767" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Transmute.20safety.20question.html#210925767">(Sep 22 2020 at 20:17)</a>:</h4>
<p>or <code>C</code></p>



<a name="210925861"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Transmute%20safety%20question/near/210925861" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lukas Wirth <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Transmute.20safety.20question.html#210925861">(Sep 22 2020 at 20:18)</a>:</h4>
<p><a href="https://doc.rust-lang.org/nomicon/other-reprs.html#reprtransparent">https://doc.rust-lang.org/nomicon/other-reprs.html#reprtransparent</a></p>



<a name="210925881"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Transmute%20safety%20question/near/210925881" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lukas Wirth <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Transmute.20safety.20question.html#210925881">(Sep 22 2020 at 20:18)</a>:</h4>
<p>Description of what that attribute does if you haven't seen it before</p>



<a name="210925974"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Transmute%20safety%20question/near/210925974" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> marmeladema <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Transmute.20safety.20question.html#210925974">(Sep 22 2020 at 20:19)</a>:</h4>
<p>Ah thanks, i've totally forgotten about <code>#[repr(transparent)]</code></p>



<a name="210926004"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Transmute%20safety%20question/near/210926004" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> marmeladema <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Transmute.20safety.20question.html#210926004">(Sep 22 2020 at 20:19)</a>:</h4>
<p>Should miri complain on my example since I don't have <code>#[repr(transparent)]</code>?</p>



<a name="210926214"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Transmute%20safety%20question/near/210926214" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> marmeladema <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Transmute.20safety.20question.html#210926214">(Sep 22 2020 at 20:21)</a>:</h4>
<p>Also:</p>
<div class="codehilite"><pre><span></span><code>This can only be used on structs with a single non-zero-sized field (there may be additional zero-sized fields). The effect is that the layout and ABI of the whole struct is guaranteed to be the same as that one field.
</code></pre></div>


<p>In my case,  I don't have a single non zero sized field, I have only one field which is unsized</p>



<a name="210926303"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Transmute%20safety%20question/near/210926303" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Transmute.20safety.20question.html#210926303">(Sep 22 2020 at 20:22)</a>:</h4>
<p>That's fine</p>



<a name="210926335"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Transmute%20safety%20question/near/210926335" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Transmute.20safety.20question.html#210926335">(Sep 22 2020 at 20:22)</a>:</h4>
<p>I think this comment is only about</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="cp">#[repr(transparent)]</span><span class="w"></span>
<span class="k">struct</span> <span class="nc">Foo</span><span class="p">(</span><span class="kt">u8</span><span class="p">,</span><span class="w"> </span><span class="kt">u8</span><span class="p">);</span><span class="w"></span>
</code></pre></div>



<a name="210926338"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Transmute%20safety%20question/near/210926338" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Transmute.20safety.20question.html#210926338">(Sep 22 2020 at 20:22)</a>:</h4>
<p>which is not allowed</p>



<a name="210926394"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Transmute%20safety%20question/near/210926394" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> marmeladema <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Transmute.20safety.20question.html#210926394">(Sep 22 2020 at 20:23)</a>:</h4>
<p>ok, so it sould be "at most a single non-zero-sized field"</p>



<a name="210926404"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Transmute%20safety%20question/near/210926404" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Aaron Hill <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Transmute.20safety.20question.html#210926404">(Sep 22 2020 at 20:23)</a>:</h4>
<p><span class="user-mention" data-user-id="281572">@marmeladema</span> In your example, you're transmuting references</p>



<a name="210926441"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Transmute%20safety%20question/near/210926441" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Aaron Hill <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Transmute.20safety.20question.html#210926441">(Sep 22 2020 at 20:23)</a>:</h4>
<p>so, while it's still insta-UB, Miri will not actually catch it, since Miri doesn't recurse through references</p>



<a name="210926456"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Transmute%20safety%20question/near/210926456" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Aaron Hill <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Transmute.20safety.20question.html#210926456">(Sep 22 2020 at 20:23)</a>:</h4>
<p>cc <span class="user-mention" data-user-id="120791">@RalfJ</span> ^</p>



<a name="210926556"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Transmute%20safety%20question/near/210926556" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> marmeladema <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Transmute.20safety.20question.html#210926556">(Sep 22 2020 at 20:24)</a>:</h4>
<p>Ok thank you for the explanations</p>



<a name="210926580"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Transmute%20safety%20question/near/210926580" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> marmeladema <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Transmute.20safety.20question.html#210926580">(Sep 22 2020 at 20:24)</a>:</h4>
<p><span class="user-mention" data-user-id="125294">@Aaron Hill</span> what does insta-UB mean exactly?</p>



<a name="210926678"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Transmute%20safety%20question/near/210926678" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Aaron Hill <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Transmute.20safety.20question.html#210926678">(Sep 22 2020 at 20:25)</a>:</h4>
<p>You've violated the validity invariant of the type, so simply constructing it is UB</p>



<a name="210926738"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Transmute%20safety%20question/near/210926738" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> marmeladema <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Transmute.20safety.20question.html#210926738">(Sep 22 2020 at 20:26)</a>:</h4>
<p>I was afraid the transmute would be UB because ref to slice are fat pointers, and ref to my custom <code>TestSlice</code> would not actually be through fat pointer</p>



<a name="210926793"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Transmute%20safety%20question/near/210926793" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Aaron Hill <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Transmute.20safety.20question.html#210926793">(Sep 22 2020 at 20:26)</a>:</h4>
<p>as opposed to something like constructing a <code>Vec</code> with a dangling pointer, which violates a safety invariant. It's not immediate UB, but must uses of it will cause UB</p>



<a name="210926805"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Transmute%20safety%20question/near/210926805" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Aaron Hill <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Transmute.20safety.20question.html#210926805">(Sep 22 2020 at 20:26)</a>:</h4>
<p>I think the nomicon goes into more detail</p>



<a name="210926887"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Transmute%20safety%20question/near/210926887" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> marmeladema <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Transmute.20safety.20question.html#210926887">(Sep 22 2020 at 20:27)</a>:</h4>
<p>Hum ok. So is it valid with <code>#[repr(transparent)]</code> or is it always wrong?</p>



<a name="210926980"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Transmute%20safety%20question/near/210926980" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Aaron Hill <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Transmute.20safety.20question.html#210926980">(Sep 22 2020 at 20:28)</a>:</h4>
<p>I believe it's valid with <code>#[repr(transparent)]</code></p>



<a name="210926993"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Transmute%20safety%20question/near/210926993" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Aaron Hill <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Transmute.20safety.20question.html#210926993">(Sep 22 2020 at 20:28)</a>:</h4>
<p>though I would check the nomicon or reference to be sure</p>



<a name="210927010"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Transmute%20safety%20question/near/210927010" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Transmute.20safety.20question.html#210927010">(Sep 22 2020 at 20:28)</a>:</h4>
<p>ref to your testslice is still a fatptr</p>



<a name="210927030"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Transmute%20safety%20question/near/210927030" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Transmute.20safety.20question.html#210927030">(Sep 22 2020 at 20:28)</a>:</h4>
<p>it should be sound</p>



<a name="210927370"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Transmute%20safety%20question/near/210927370" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> marmeladema <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Transmute.20safety.20question.html#210927370">(Sep 22 2020 at 20:31)</a>:</h4>
<p>Thanks a lot for the help :)<br>
The why I am doing that is to bypass some current limitation in rustc/std with<code>Cow</code>.<br>
Ideally what I would do is: </p>
<div class="codehilite"><pre><span></span><code>#[derive(Clone, Debug)]
enum Test &lt;&#39;a&gt; {
    Int(u8),
    Array(Cow&lt;&#39;a, [Test&lt;&#39;a&gt;]&gt;),
}
</code></pre></div>


<p>Link: <a href="https://play.rust-lang.org/?version=stable&amp;mode=debug&amp;edition=2018&amp;gist=11ba66d167e152eeb13de374112f0537">https://play.rust-lang.org/?version=stable&amp;mode=debug&amp;edition=2018&amp;gist=11ba66d167e152eeb13de374112f0537</a><br>
But I get a <code>overflow evaluating the requirement </code>Test&lt;'a&gt;: std::marker::Sized` error</p>



<a name="210927423"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Transmute%20safety%20question/near/210927423" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Philipp Korber <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Transmute.20safety.20question.html#210927423">(Sep 22 2020 at 20:31)</a>:</h4>
<p>I just realized that the behaviour of <code>#[repr(transparent)]</code> isn't well defined on zero-sized struct.</p>
<p>Because of the "single non-zero-sized field".</p>
<p>We probably should change it to "at most one non-zero-sized field"?</p>



<a name="210927497"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Transmute%20safety%20question/near/210927497" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Aaron Hill <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Transmute.20safety.20question.html#210927497">(Sep 22 2020 at 20:32)</a>:</h4>
<p><span class="user-mention" data-user-id="281572">@marmeladema</span> You'll need to introduce some kind of indirection (e.g. a <code>Box</code>)</p>



<a name="210927566"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Transmute%20safety%20question/near/210927566" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Aaron Hill <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Transmute.20safety.20question.html#210927566">(Sep 22 2020 at 20:32)</a>:</h4>
<p>since <code>Test</code> will end up containing itself directly</p>



<a name="210927577"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Transmute%20safety%20question/near/210927577" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> marmeladema <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Transmute.20safety.20question.html#210927577">(Sep 22 2020 at 20:33)</a>:</h4>
<p>Well, manually defining my Cow type works, or using the trick, this thread is about also seem to work</p>



<a name="210927621"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Transmute%20safety%20question/near/210927621" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> marmeladema <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Transmute.20safety.20question.html#210927621">(Sep 22 2020 at 20:33)</a>:</h4>
<p>oh the code I pasted is wrong</p>



<a name="210927769"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Transmute%20safety%20question/near/210927769" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Aaron Hill <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Transmute.20safety.20question.html#210927769">(Sep 22 2020 at 20:34)</a>:</h4>
<p>there's a nicer error we should be emitting there, but we aren't for some reason. I'll investigate</p>



<a name="210927862"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Transmute%20safety%20question/near/210927862" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> marmeladema <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Transmute.20safety.20question.html#210927862">(Sep 22 2020 at 20:35)</a>:</h4>
<p><a href="https://play.rust-lang.org/?version=stable&amp;mode=debug&amp;edition=2018&amp;gist=b1fa4a65e92987aa92b9d545f2879224">https://play.rust-lang.org/?version=stable&amp;mode=debug&amp;edition=2018&amp;gist=b1fa4a65e92987aa92b9d545f2879224</a></p>



<a name="210927919"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Transmute%20safety%20question/near/210927919" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> marmeladema <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Transmute.20safety.20question.html#210927919">(Sep 22 2020 at 20:35)</a>:</h4>
<p>This is what I really want to do. It should work without a <code>Box</code>, because the Cow enum will contain either a <code>&amp;[Test]</code> or a <code>Vec&lt;Test&gt;</code></p>



<a name="210927994"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Transmute%20safety%20question/near/210927994" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Transmute.20safety.20question.html#210927994">(Sep 22 2020 at 20:36)</a>:</h4>
<p>that's not what cow does, [T]::Owned isn't Vec&lt;T&gt; afaik</p>



<a name="210928043"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Transmute%20safety%20question/near/210928043" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Aaron Hill <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Transmute.20safety.20question.html#210928043">(Sep 22 2020 at 20:36)</a>:</h4>
<p>I think it is</p>



<a name="210928056"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Transmute%20safety%20question/near/210928056" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Aaron Hill <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Transmute.20safety.20question.html#210928056">(Sep 22 2020 at 20:36)</a>:</h4>
<p><a href="https://doc.rust-lang.org/beta/std/borrow/trait.ToOwned.html#impl-ToOwned-4">https://doc.rust-lang.org/beta/std/borrow/trait.ToOwned.html#impl-ToOwned-4</a></p>



<a name="210928067"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Transmute%20safety%20question/near/210928067" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Transmute.20safety.20question.html#210928067">(Sep 22 2020 at 20:36)</a>:</h4>
<p>oh hm</p>



<a name="210928075"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Transmute%20safety%20question/near/210928075" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Transmute.20safety.20question.html#210928075">(Sep 22 2020 at 20:36)</a>:</h4>
<p>I guess it is!</p>



<a name="210928084"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Transmute%20safety%20question/near/210928084" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Aaron Hill <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Transmute.20safety.20question.html#210928084">(Sep 22 2020 at 20:36)</a>:</h4>
<p>so, I guess it's not actually infinitely sized</p>



<a name="210928223"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Transmute%20safety%20question/near/210928223" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Transmute.20safety.20question.html#210928223">(Sep 22 2020 at 20:38)</a>:</h4>
<p>hm so I think the error is actually about a different problem</p>



<a name="210928316"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Transmute%20safety%20question/near/210928316" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Transmute.20safety.20question.html#210928316">(Sep 22 2020 at 20:38)</a>:</h4>
<p>i.e., it's just a genuine recursive dep</p>



<a name="210928356"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Transmute%20safety%20question/near/210928356" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Transmute.20safety.20question.html#210928356">(Sep 22 2020 at 20:39)</a>:</h4>
<p>we're probably not properly canonicalizing the value somewhere, so we recurse instead of detecting that <code>&lt;B as ToOwned&gt;::Owned</code> is Sized for any B</p>



<a name="210928807"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Transmute%20safety%20question/near/210928807" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> marmeladema <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Transmute.20safety.20question.html#210928807">(Sep 22 2020 at 20:43)</a>:</h4>
<p>I don't have enough knowledge about the compiler to fix it I am afraid <span aria-label="grimacing" class="emoji emoji-1f62c" role="img" title="grimacing">:grimacing:</span></p>



<a name="210936845"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Transmute%20safety%20question/near/210936845" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Daniel Henry-Mantilla <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Transmute.20safety.20question.html#210936845">(Sep 22 2020 at 22:01)</a>:</h4>
<p>Note that <code>#[repr(C)]</code> suffices here. Also, see <a href="https://docs.rs/ref-cast">https://docs.rs/ref-cast</a></p>



<a name="211158218"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Transmute%20safety%20question/near/211158218" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Transmute.20safety.20question.html#211158218">(Sep 24 2020 at 16:44)</a>:</h4>
<p><span class="user-mention silent" data-user-id="125294">Aaron Hill</span> <a href="#narrow/stream/122651-general/topic/Transmute.20safety.20question/near/210926441">said</a>:</p>
<blockquote>
<p>so, while it's still insta-UB, Miri will not actually catch it, since Miri doesn't recurse through references</p>
</blockquote>
<p>no it is actually not UB. see <a href="https://doc.rust-lang.org/reference/behavior-considered-undefined.html">this list for what is UB</a>.<br>
however, this code relies on an assumption that is not guaranteed by the language, namely, that <code>TestSlice</code> is laid out in a particular way. so the code relies on <em>unspecified behavior</em>, but there is no UB. the code actually has well-defined meaning <em>if the assumption is correct</em>, and it is correct for current stable Rust, but without <code>repr(transparent)</code> or <code>repr(C)</code> it might stop being correct in the future.</p>



<a name="211158353"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Transmute%20safety%20question/near/211158353" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Transmute.20safety.20question.html#211158353">(Sep 24 2020 at 16:46)</a>:</h4>
<p>It would be nice to be able to detect code that relies on unspecified behavior, but it is non-trivial... probably the best way would be to add a <code>-Zrandomize-layout</code> flag to rustc that does all sorts of crazy things to layout that would "hopefully" lead to actual UB if the code made wrong assumptions.</p>



<a name="211318822"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Transmute%20safety%20question/near/211318822" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> marmeladema <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Transmute.20safety.20question.html#211318822">(Sep 25 2020 at 21:15)</a>:</h4>
<p>If I wanted to fix the compile error for <code>overflow evaluating the requirement Test&lt;'a&gt;: std::marker::Sized</code> where should I start looking? I've ever only done refactoring in the compiler, never actually enhanced it, so its a bit of a new territory to me.</p>



<a name="211353082"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Transmute%20safety%20question/near/211353082" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Elichai Turkel <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Transmute.20safety.20question.html#211353082">(Sep 26 2020 at 10:11)</a>:</h4>
<p><span class="user-mention silent" data-user-id="120791">RalfJ</span> <a href="#narrow/stream/122651-general/topic/Transmute.20safety.20question/near/211158353">said</a>:</p>
<blockquote>
<p>It would be nice to be able to detect code that relies on unspecified behavior, but it is non-trivial... probably the best way would be to add a <code>-Zrandomize-layout</code> flag to rustc that does all sorts of crazy things to layout that would "hopefully" lead to actual UB if the code made wrong assumptions.</p>
</blockquote>
<p>Can't rustc "just" add(under a -ZSomething) some padding bytes at the start of all <code>repr(Rust)</code> structs? that should break almost everything relying on this, I think relying on an exact size/alignment is also unspecified behavior</p>



<a name="211353546"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Transmute%20safety%20question/near/211353546" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Transmute.20safety.20question.html#211353546">(Sep 26 2020 at 10:23)</a>:</h4>
<p><span class="user-mention silent" data-user-id="249222">Elichai Turkel</span> <a href="#narrow/stream/122651-general/topic/Transmute.20safety.20question/near/211353082">said</a>:</p>
<blockquote>
<p><span class="user-mention silent" data-user-id="120791">RalfJ</span> <a href="#narrow/stream/122651-general/topic/Transmute.20safety.20question/near/211158353">said</a>:</p>
<blockquote>
<p>It would be nice to be able to detect code that relies on unspecified behavior, but it is non-trivial... probably the best way would be to add a <code>-Zrandomize-layout</code> flag to rustc that does all sorts of crazy things to layout that would "hopefully" lead to actual UB if the code made wrong assumptions.</p>
</blockquote>
<p>Can't rustc "just" add(under a -ZSomething) some padding bytes at the start of all <code>repr(Rust)</code> structs? that should break almost everything relying on this, I think relying on an exact size/alignment is also unspecified behavior</p>
</blockquote>
<p>yeah that seems like a reasonable first step</p>



<a name="211353591"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Transmute%20safety%20question/near/211353591" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Transmute.20safety.20question.html#211353591">(Sep 26 2020 at 10:24)</a>:</h4>
<p>it should also do that for <code>repr(Rust)</code> unions FWIW, and add different amounts of padding to different fields</p>



<a name="211353593"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Transmute%20safety%20question/near/211353593" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Transmute.20safety.20question.html#211353593">(Sep 26 2020 at 10:24)</a>:</h4>
<p>it is very common to forget <code>repr(C)</code> when using unions to transmute things</p>



<a name="211353608"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Transmute%20safety%20question/near/211353608" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Transmute.20safety.20question.html#211353608">(Sep 26 2020 at 10:25)</a>:</h4>
<p>do you want to open an issue for this? seems like a reasonable request</p>



<a name="211562738"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/Transmute%20safety%20question/near/211562738" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> marmeladema <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/Transmute.20safety.20question.html#211562738">(Sep 28 2020 at 23:17)</a>:</h4>
<p><span class="user-mention" data-user-id="120791">@RalfJ</span> I took the liberty to open <a href="https://github.com/rust-lang/rust/issues/77316">https://github.com/rust-lang/rust/issues/77316</a> with a very brief description. Feel free to improve it <span aria-label="+1" class="emoji emoji-1f44d" role="img" title="+1">:+1:</span> <br>
I'd be glag to work on it even though, I've no idea where to start ^^</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>